package cn.taoci.hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private DBService dbService;

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public boolean save(
            @RequestParam(value = "username") String username,
            @RequestParam(value = "sex") String sex,
            @RequestParam(value = "job") String job,
            @RequestParam(value = "phoneNumber") String phoneNumber,
            @RequestParam(value = "organizationName") String organizationName) {
        boolean success = Boolean.FALSE;
        try {
            dbService.save(username, sex, job, phoneNumber, organizationName);
            success = Boolean.TRUE;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return success;
    }

    /**
     * 记录访问量
     */
    @RequestMapping(value = "/accessCounter", method = RequestMethod.GET)
    public long accessCounter() {
        long counter = 0;
        try {
            counter = dbService.accessCount();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return counter;

    }
}
